package com.example.controller;

import com.example.pojo.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("students", studentService.getAllStudents());
        return "studentList";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(Student student) {
        studentService.addStudent(student);
        return "redirect:/student/list";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(int id) {
        studentService.deleteStudent(id);
        return "redirect:/student/list";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Student student) {
        studentService.updateStudent(student);
        return "redirect:/student/list";
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(int id, Model model) {
        Student student = studentService.getStudentById(id);
        model.addAttribute("student", student);
        return "editStudent";
    }

    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd() {
        return "addStudent";
    }
}
